package com.gitee.wsl.common.more.ext.net

import android.content.Context
import com.gitee.wsl.jvm.net.server.IHTTPSession
import com.gitee.wsl.jvm.net.server.Response
import com.gitee.wsl.struct.router.Method
import io.ktor.server.application.ApplicationCall
import io.ktor.server.request.ApplicationRequest
import io.ktor.server.response.ApplicationResponse
import io.ktor.utils.io.jvm.javaio.toInputStream
import java.io.InputStream
import java.net.CookieHandler

/*
class KtorHttpSession(val request:AsyncHttpServerRequest, val response:AsyncHttpServerResponse,val serverContext: Context):
    IHTTPSession {

    override val context: Context
        get() = serverContext
    override val cookies: CookieHandler
        get() = CookieHandler.getDefault()
    override val headers: Map<String, String>
        get() = request.headers.multiMap.toSingleMap()
    override val method: Method
        get() = Method.lookup(request.method)!!
    override val parameters: Map<String, List<String>>
        get() = TODO("Not yet implemented")
    override val query: Map<String, String>
        get() = request.query.toSingleMap()
    override val uri: String
        get() = request.url.toString()
    override val remoteIpAddress: String
        get() = ""
    override val requestBody: String
        get() = request.getBody<StringBody>().get()
    override val path: String
        get() = request.path

}

fun HttpRouterServer.route(session: io.ktor.server.response.ApplicationResponse) {
    Timber.d("come in route:" + session.uri + "method:" + session.method + "\t path:" + session.path)
    if (!doHandler(session)) {
        val resp = service(session)
        session.response.setContentType(resp.mimeType)
        session.response.code(resp.status.requestStatus)
        session.response.sendStream(resp.data, resp.contentLength)
    }
}*/

class KtorHttpSession(val application: ApplicationCall,
                      val ktorRequest: ApplicationRequest=application.request,
                      val ktorResponse: ApplicationResponse=application.response,
                      val serverContext: Context
): IHTTPSession {

    override var response: Response?=null

    override val inputStream: InputStream get() = ktorRequest.receiveChannel().toInputStream()
    /*override val context: Context
        get() = serverContext*/
    override val cookies: CookieHandler
        get() = TODO("Not yet implemented")
    override val headers: Map<String, String>
        get() = TODO("Not yet implemented")
    override val method: Method
        get() = TODO("Not yet implemented")
    override val parameters: Map<String, List<String>>
        get() = TODO("Not yet implemented")
    override val query: Map<String, String>
        get() = TODO("Not yet implemented")
    override val uri: String
        get() = TODO("Not yet implemented")
    override val remoteIpAddress: String
        get() = TODO("Not yet implemented")
    override val requestBody: String
        get() = TODO("Not yet implemented")
    override val path: String
        get() = TODO("Not yet implemented")
}


